<script lang="ts" setup>
  import { ref } from "vue"
  import gmDialog from "@/components/Dialog/index.vue"
  import { cloneDeep } from "lodash-es"
  import { saveOrUpdateDepartment } from "@/api/department"
  import { type FormInstance, type FormRules } from "element-plus"
  import useForm from "@/hooks/useForm"

  interface Props {
    data?: any
  }

  const props = withDefaults(defineProps<Props>(), {
    data: () => ({}),
  })

  const form = ref<any>(cloneDeep(props.data) || {})
  const formRef = ref<FormInstance>()
  const dialogRef = ref<InstanceType<typeof gmDialog>>()
  const emit = defineEmits(["cancel", "success"])

  const rules: FormRules = {
    name: [{ required: true, message: "请输入名称", trigger: "change" }],
  }

  const { handleSubmit } = useForm({ formType: "dialog", emit: emit })

  const cancel = () => {
    emit("cancel")
  }

  const success = () => {
    handleSubmit(saveOrUpdateDepartment, formRef.value, form.value, dialogRef.value)
  }
</script>

<template>
  <gm-dialog ref="dialogRef" title="新增/编辑部门系列" @cancel="cancel" @comfirm="success">
    <el-form ref="formRef" :model="form" :rules="rules" label-width="120px" label-position="left">
      <el-form-item prop="name" label="名称">
        <el-input v-model="form.name" placeholder="请输入" />
      </el-form-item>
      <el-form-item prop="manageName" label="负责人">
        <el-input v-model="form.manageName" placeholder="请输入" />
      </el-form-item>
      <el-form-item prop="manageMobile" label="负责人联系方式">
        <el-input v-model="form.manageMobile" placeholder="请输入" />
      </el-form-item>
    </el-form>
  </gm-dialog>
</template>

<style lang="scss" scoped>
</style>
